package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;


import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.FrequencyTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.constant.SeparatorConstant;
import com.wtwd.device.model.BloodOxygen;
import com.wtwd.device.model.HeartRate;
import com.wtwd.device.model.vo.AppFrequencyVo;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.DeviceBloodOxygen;
import com.wtwd.wtwddevice.model.entity.DeviceHeartRate;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceBloodOxygenService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IFrequencyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 设置血氧频率回复
 */
@MsgType(MsgTypeEnum.W_OXTSTART)
@Component
public class SetWOXTStartHandlerImpl implements IMsgHandler {

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IDeviceBloodOxygenService deviceBloodOxygenService;

    @Resource
    private IFrequencyService frequencyService;

    @Resource
    private IAppCommService appCommService;

    @Override
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        String body = wtProtocol.getBody();
        String[] dataArray = body.split(SeparatorConstant.POUND_SIGN);
        Integer bloodOxygen = Integer.valueOf(dataArray[0]);
        if (bloodOxygen.equals(0)) {
            return;
        }
        String isWear = dataArray[1];
        Long deviceId = deviceService.getDeviceIdByImei(wtProtocol.getImei());
        DeviceBloodOxygen deviceBloodOxygen = new DeviceBloodOxygen();
        deviceBloodOxygen.setDeviceId(deviceId);
        deviceBloodOxygen.setValue(bloodOxygen);
        deviceBloodOxygen.setTime(wtProtocol.getTime());
        deviceBloodOxygen.setNumber(wtProtocol.getNumber());
        deviceBloodOxygen.setType(messageBase.getType());
        deviceBloodOxygen.setIsWear(isWear);
        deviceBloodOxygen.setCreateTime(new Date().getTime());
        deviceBloodOxygen.setUpdateTime(new Date().getTime());
        deviceBloodOxygenService.insert(deviceBloodOxygen);
        frequencyService.updateState(deviceId, FrequencyTypeEnum.W_OXTSTART.getType(), CmdStateEnum.SUCCESS.getState());
        //推送到app
        BloodOxygen data = new BloodOxygen();
        BeanUtils.copyProperties(deviceBloodOxygen, data);
        AppFrequencyVo appFrequencyVo = new AppFrequencyVo();
        appFrequencyVo.setType(FrequencyTypeEnum.W_OXTSTART.getType());
        appFrequencyVo.setValue(data);
        AppProtocol appProtocol = AppProtocol.builder()
                .imei(wtProtocol.getImei())
                .cmdName(MsgTypeEnum.APP_SET_FREQUENCY.getCmdName())
                .data(appFrequencyVo)
                .build();
        appCommService.pushDeviceState(deviceId, appProtocol);
    }


}
